using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MKDS_Course_Editor
{
    public partial class mission : Form
    {
        nkm_file.mr mr = new nkm_file.mr();
        public static string path_ = "";
        public mission(string path)
        {
            path_ = path;
            InitializeComponent();
        }

        private void mission_Load(object sender, EventArgs e)
        {
            System.IO.BinaryReader br = new System.IO.BinaryReader(System.IO.File.OpenRead(path_));
            if (br.ReadChar().ToString() + br.ReadChar().ToString() + br.ReadChar().ToString() + br.ReadChar().ToString() == "NKMR")
            {
                br.BaseStream.Position = 0;
                /*mr.time_1[0] = br.ReadByte();
                mr.time_2[0] = br.ReadByte();
                br.ReadBytes(5);
                mr.fig[0] = Convert.ToInt32(br.ReadByte());
                mr.circuit[0] = br.ReadBytes(4);
                br.ReadBytes(5);
                mr.number[0] = Convert.ToInt32(br.ReadByte());
                br.ReadBytes(18);
                mr.name[0] = br.ReadChar().ToString() + br.ReadChar().ToString() + br.ReadChar().ToString() + br.ReadChar().ToString();
                listBox1.Items.Add(mr.name[0]);
                //comboBox1.SelectedIndex = mr.fig[0];*/
                for (int i = 0; i < 91; i++)
                {
                    br.ReadBytes(8);
                    mr.time_1[i] = br.ReadByte();
                    mr.time_2[i] = br.ReadByte();
                    br.ReadBytes(6);
                    mr.circuit[i] = br.ReadByte();
                    br.ReadByte();
                    mr.fig[i] = Convert.ToInt32(br.ReadByte());
                    br.ReadBytes(4);
                    mr.number[i] = Convert.ToInt32(br.ReadByte());
                    br.ReadBytes(16);
                    mr.name[i] = br.ReadChar().ToString() + br.ReadChar().ToString() + br.ReadChar().ToString() + br.ReadChar().ToString();
                    listBox1.Items.Add(mr.name[i]);
                }
                listBox1.SelectedIndex = 0;
            }
            else
            {
                MessageBox.Show("It isn't a MR file or it is corrupt.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                br.Close();
            }
            br.Close();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            byte[] s = {mr.time_1[listBox1.SelectedIndex],mr.time_2[listBox1.SelectedIndex] };
            int ms = parselittle(BitConverter.ToString(s).Replace("-","")+"0000");
            numericUpDown1.Value = TimeSpan.FromMilliseconds((double)Convert.ToDecimal(ms*1000)).Hours;
            numericUpDown2.Value = TimeSpan.FromMilliseconds((double)Convert.ToDecimal(ms * 1000)).Minutes;
            numericUpDown3.Value = TimeSpan.FromMilliseconds((double)Convert.ToDecimal(ms * 1000)).Seconds;
            numericUpDown4.Value = mr.number[listBox1.SelectedIndex];
            comboBox1.SelectedIndex = mr.fig[listBox1.SelectedIndex];
            comboBox2.SelectedIndex = mr.circuit[listBox1.SelectedIndex];
        }
            int parselittle(string input)
        {
            int test = 0;
            test = int.Parse(input.ToCharArray()[6].ToString() + input.ToCharArray()[7].ToString() + input.ToCharArray()[4].ToString() + input.ToCharArray()[5].ToString() + input.ToCharArray()[2].ToString() + input.ToCharArray()[3].ToString() + input.ToCharArray()[0].ToString() + input.ToCharArray()[1].ToString(), System.Globalization.NumberStyles.HexNumber);
            return test;
        }

            private void numericUpDown1_ValueChanged(object sender, EventArgs e)
            {
                byte[] s = {mr.time_1[listBox1.SelectedIndex],mr.time_2[listBox1.SelectedIndex] };
            int ms = parselittle(BitConverter.ToString(s).Replace("-","")+"0000");
            if (numericUpDown1.Value != TimeSpan.FromMilliseconds((double)Convert.ToDecimal(ms * 1000)).Hours)
            {
                TimeSpan t = new TimeSpan((int)numericUpDown1.Value, (int)numericUpDown2.Value, (int)numericUpDown3.Value);
                byte[] q = new byte[]{};
                q = Int32ToBytes((int)t.TotalMilliseconds / 1000);
                mr.time_1[listBox1.SelectedIndex] = q[0];
                mr.time_2[listBox1.SelectedIndex] = q[1];
            }
            }
            private static byte[] Int32ToBytes(Int32 int32Input)
            {
                byte[] numBytesBuf = new byte[4];
                return System.BitConverter.GetBytes(int32Input);
            }

            private void numericUpDown2_ValueChanged(object sender, EventArgs e)
            {
                byte[] s = { mr.time_1[listBox1.SelectedIndex], mr.time_2[listBox1.SelectedIndex] };
                int ms = parselittle(BitConverter.ToString(s).Replace("-", "") + "0000");
                if (numericUpDown2.Value != TimeSpan.FromMilliseconds((double)Convert.ToDecimal(ms * 1000)).Minutes)
                {
                    TimeSpan t = new TimeSpan((int)numericUpDown1.Value, (int)numericUpDown2.Value, (int)numericUpDown3.Value);
                    byte[] q = new byte[] { };
                    q = Int32ToBytes((int)t.TotalMilliseconds / 1000);
                    mr.time_1[listBox1.SelectedIndex] = q[0];
                    mr.time_2[listBox1.SelectedIndex] = q[1];
                }
            }

            private void numericUpDown3_ValueChanged(object sender, EventArgs e)
            {
                byte[] s = { mr.time_1[listBox1.SelectedIndex], mr.time_2[listBox1.SelectedIndex] };
                int ms = parselittle(BitConverter.ToString(s).Replace("-", "") + "0000");
                if (numericUpDown3.Value != TimeSpan.FromMilliseconds((double)Convert.ToDecimal(ms * 1000)).Seconds)
                {
                    TimeSpan t = new TimeSpan((int)numericUpDown1.Value, (int)numericUpDown2.Value, (int)numericUpDown3.Value);
                    byte[] q = new byte[] { };
                    q = Int32ToBytes((int)t.TotalMilliseconds / 1000);
                    mr.time_1[listBox1.SelectedIndex] = q[0];
                    mr.time_2[listBox1.SelectedIndex] = q[1];
                }
            }

            private void numericUpDown4_ValueChanged(object sender, EventArgs e)
            {
                byte[] s = { mr.time_1[listBox1.SelectedIndex], mr.time_2[listBox1.SelectedIndex] };
                int ms = parselittle(BitConverter.ToString(s).Replace("-", "") + "0000");
                if (numericUpDown4.Value != mr.number[listBox1.SelectedIndex])
                {
                    TimeSpan t = new TimeSpan((int)numericUpDown1.Value, (int)numericUpDown2.Value, (int)numericUpDown3.Value);
                    byte[] q = new byte[] { };
                    q = Int32ToBytes((int)numericUpDown4.Value);
                    mr.number[listBox1.SelectedIndex] = q[0];
                }
            }

            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (comboBox1.SelectedIndex != mr.fig[listBox1.SelectedIndex])
                {
                    mr.fig[listBox1.SelectedIndex] = comboBox1.SelectedIndex;
                }
            }

            private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (comboBox2.SelectedIndex != mr.circuit[listBox1.SelectedIndex])
                {
                    mr.circuit[listBox1.SelectedIndex] = (byte)comboBox1.SelectedIndex;
                }
            }

            private void button1_Click(object sender, EventArgs e)
            {
                
            } 
    }
}
